package mylibrary.security;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class AES {

	public static void main(String[] args) throws Exception {
		
		
		KeyGenerator kg = KeyGenerator.getInstance("AES");
		kg.init(256);
		SecretKey sk = kg.generateKey();
		sk.getEncoded();
		
		System.out.println(1);
		
		
		
		
		
		
		KeyGenerator kgen = KeyGenerator.getInstance("AES");		//获取密钥生成器
		kgen.init(128);
		SecretKey secretKey = kgen.generateKey();					//生成密钥
		
		Cipher cp = Cipher.getInstance("AES/CBC/NoPadding");						//创建密码器
		cp.init(Cipher.ENCRYPT_MODE, secretKey);
		System.out.println(cp.getIV());
		System.out.println("1 " + new String(cp.getIV(), "utf8"));
		
		String str = "content";
		byte[] pt = str.getBytes("utf8");
		byte[] ct = cp.doFinal(pt);				//加密
		System.out.println(new String(ct, "utf8"));
		
		
		Cipher cp1 = Cipher.getInstance("AES");
		cp1.init(Cipher.DECRYPT_MODE, secretKey);
		byte[] ptext = cp1.doFinal(ct);			//解密
		System.out.println(new String(ptext, "utf8"));
		
	}
}
